﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CLUBsInterpreter.ObjectTypes;

namespace CLUBsInterpreter.AST
{
	/**
	 * <summary>
	 * Describes a block in the AST.
	 * </summary>
	 **/
	class BlockNode : AbstractSyntaxTree
	{
		// Tree Structure
		/**
		 * <summary>
		 * The statements contained within this block.
		 * </summary>
		 **/
		public List<StatementNode> Statements = new List<StatementNode>();
		
		// Tree Decoration
		/**
		 * <summary>
		 * The name of this block. If the block is a definition, this will be the identifier of the definition.
		 * If this is a local block in a control statement, it will have an auto-generated unique identifier.
		 * </summary>
		 **/
		public string BlockName = null;
		/**
		 * <summary>
		 * Contains the C# delegate to be executed when the block is to be executed.
		 * </summary>
		 **/
		public Action TranslatedMethod = null;
		public Action ReturnMethod = null;

		public Scope InternalScope = new Scope();
		
		public override void Visit(Visitor v)
		{
			// Call the appropriate Visitor method
			if (m_visitedOnce)
				v.VisitBlockNodeSecond(this);
			else
			{
				m_visitedOnce = true;
				v.VisitBlockNode(this);
			}
		}

		/**
		 * <summary>
		 * Prints this block and all statements within.
		 * </summary>
		 **/
		public override void PrintTree(TextWriter outStream, int depth)
		{
			// Write the blocknode
			outStream.WriteLine(AbstractSyntaxTree.TabDepth(depth) + "BlockNode");
			// Call PrintTree for all substatements
			foreach (StatementNode node in Statements)
			{
				node.PrintTree(outStream, depth+1);
			}
		}
	}
}
